package com.applcn.user.repo.repository;

import com.applcn.user.constant.MenuTypeEnum;
import com.applcn.user.repo.entity.MenuEntity;
import com.applcn.user.repo.mapper.MenuMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * com.applcn.user.repo.repository
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-03-09 00:17
 */
@Repository
@RequiredArgsConstructor
public class MenuRepository extends ServiceImpl<MenuMapper, MenuEntity> {

    /**
     * 根据父菜单id获取菜单列表
     *
     * @param shopId
     * @param parentId
     * @param menuType
     * @return
     */
    public List<MenuEntity> menuList(Long shopId, Long parentId, MenuTypeEnum menuType) {
        LambdaQueryWrapper<MenuEntity> wrapper = new QueryWrapper<MenuEntity>().lambda();
        if (menuType != null) {
            wrapper.eq(MenuEntity::getMenuType, menuType);
        }
        wrapper.eq(MenuEntity::getShopId, shopId).eq(MenuEntity::getParentId, parentId).orderByAsc(MenuEntity::getSort);
        List<MenuEntity> menuEntityList = baseMapper.selectList(
                wrapper
        );
        return menuEntityList;
    }

    /**
     * 批量删除目录
     *
     * @param ids
     * @param shopId
     */
    public void deleteBatch(List<Long> ids, Long shopId) {
        baseMapper.delete(
                new QueryWrapper<MenuEntity>()
                        .lambda()
                        .in(MenuEntity::getId, ids)
                        .eq(MenuEntity::getShopId, shopId)
        );
    }

    /**
     * 通过shopId查找菜单
     *
     * @param shopId
     * @return
     */
    public List<MenuEntity> listByShopId(Long shopId) {
        return baseMapper.selectList(
                new QueryWrapper<MenuEntity>()
                .lambda()
                .eq(MenuEntity::getShopId, shopId)
        );
    }

    /**
     * 统计子菜单数量
     * @param parentId
     * @param menuType
     * @return
     */
    public Integer countChildren(Long parentId, MenuTypeEnum menuType) {
        LambdaQueryWrapper<MenuEntity> wrapper = new QueryWrapper<MenuEntity>().lambda();
        wrapper.eq(MenuEntity::getParentId, parentId);
        if (menuType != null) {
            wrapper.eq(MenuEntity::getMenuType, menuType);
        }
        return baseMapper.selectCount(
                wrapper
        );
    }
}
